package com.yunji.order.context.builder;

import com.yunji.base.cache.BaseCache;
import com.yunji.base.dto.AgentDTO;
import com.yunji.common.exception.ServiceException;
import com.yunji.common.model.cache.CabinetCacheDTO;
import com.yunji.common.model.domain.SiteDO;
import com.yunji.common.model.domain.SiteLocationDO;
import com.yunji.common.model.domain.StaffDO;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.coupon.dto.OrderAmtDTO;
import com.yunji.order.domain.OrderDO;
import com.yunji.system.context.SystemContext;

/**
 * @author : peter-zhu
 * @date : 2025/3/8 09:47
 * @description : TODO
 **/
public class OrderBuilder {
    private final OrderDO order;

    private OrderBuilder() {
        this.order = new OrderDO();
    }

    public static OrderBuilder builder() {
        return new OrderBuilder();
    }

    public OrderBuilder buildMember() {
        LoginInfo loginInfo = SystemContext.getUser();
        order.setMemberId(loginInfo.getUserId());
        order.setMemberName(loginInfo.getNickname());
        order.setMemberPhone(loginInfo.getPhone());
        order.setOpenId(loginInfo.getOpenId());
        return this;
    }

    public OrderBuilder buildContainer(CabinetCacheDTO cabinet) {
        ServiceException.throwException(cabinet == null, "容器为空");
        order.setQrCode(cabinet.getQrcode());
        order.setContainerId(cabinet.getContainerId());
        order.setContainerName(cabinet.getContainerName());
        order.setContainerType(cabinet.getTypeCode());
        order.setContainerTypeName(cabinet.getTypeName());
        order.setChipSn(cabinet.getChipSn());
        order.setChipImei(cabinet.getChipImei());
        order.setBelongId(cabinet.getBelongId());
        return buildStaff(BaseCache.getStaff(cabinet.getStaffId()))
                .buildAgent(BaseCache.getAgent(cabinet.getAgentId()))
                .buildSite(BaseCache.getSite(cabinet.getSiteId()))
                .buildLocation(BaseCache.getSiteLocation(cabinet.getLocationId()));
    }

    private OrderBuilder buildStaff(StaffDO staff) {
        ServiceException.throwException(staff == null, "员工数据为空");
        order.setStaffId(staff.getId());
        order.setStaffTel(staff.getStaffTel());
        order.setStaffName(staff.getStaffName());
        return this;
    }

    private OrderBuilder buildAgent(AgentDTO agent) {
        ServiceException.throwException(agent == null, "代理商为空");
        order.setAgentId(agent.getId());
        order.setAgentName(agent.getAgentName());
        order.setAgentPhone(agent.getAgentTel());
        return this;
    }

    private OrderBuilder buildSite(SiteDO site) {
        ServiceException.throwException(site == null, "网点为空");
        order.setProvinceCode(site.getProvinceCode());
        order.setProvinceName(site.getProvinceName());
        order.setCityCode(site.getCityCode());
        order.setCityName(site.getCityName());
        order.setCountyCode(site.getCountyCode());
        order.setCountyName(site.getCountyName());
        order.setRegion(site.getRegion());
        order.setAddress(site.getAddress());
        order.setSiteId(site.getId());
        order.setSiteName(site.getSiteName());
        order.setSiteTel(site.getSiteTel());
        order.setContactName(site.getSiteContacts());
        order.setContactPhone(site.getSiteTel());
        return this;
    }

    private OrderBuilder buildLocation(SiteLocationDO siteLocation) {
        ServiceException.throwException(siteLocation == null, "点位为空");
        order.setLocationId(siteLocation.getLocationId());
        order.setLocationName(siteLocation.getLocationName());
        order.setLocationTypeId(siteLocation.getLocationTypeId());
        order.setLocationTypeName(BaseCache.getSiteLocationTypeName(siteLocation.getLocationTypeId()));
        return this;
    }

    public OrderBuilder buildAmt(OrderAmtDTO orderAmt) {
        order.setCouponAmount(orderAmt.getCoupleAmount());
        order.setTotalAmount(orderAmt.getPrice());
        order.setActualAmount(orderAmt.getPayableAmount());
        order.setSubtractAmount(orderAmt.getSubtractAmount());
        order.setSiteSalePrice(orderAmt.getDiscountAmount());
        order.setSalePrice(orderAmt.getPrice());
        order.setCostPrice(orderAmt.getCostPrice());
        return this;
    }

    public OrderDO build() {
        return this.order;
    }
}
